package com.confess.DAO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.confess.ofyservice.OfyService;
import com.confess.model.Confession;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.Query;

public class AnonConfessionDAOImpl implements AnonConfessionDAO {

	private Objectify objectify = OfyService.ofy();
	
	private static final int limit = 20;
	
	@Override
	public void storeConfession(Confession confession) {
		objectify.put(confession);
	}
	
	@Override
	public List<Confession> getConfessions(int pageNum) {
		List<Confession> confessions = new ArrayList<Confession>();
		//need to add an offset.
		Query<Confession> query = objectify.query(Confession.class).order("-date").limit(limit).offset(limit*pageNum);
		
		for (Confession conf: query) {
			confessions.add(conf);
		}
		
		return confessions;
	}
	
	@Override
	public List<Confession> getNewConfessions(Date date) {
		List<Confession> confessions = new ArrayList<Confession>();

		Query<Confession> query = objectify.query(Confession.class).filter("date >",date);
		
		for (Confession conf: query) {
			confessions.add(conf);
		}
		
		return confessions;
	}
}
